/*******************************************************************************
 * Copyright 2020 huanggefan.cn
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/

const tools = require("../js/tools");

window.Action_chapter_Create = function (course, index, name, description) {
    let request = tools.DeepCopy(window.API_Request_Chapter_CreateChapter);
    request.Name = name;
    request.Description = description;
    request.ManagerUUID = window.AuthTokenInfo.uuid;
    request.CourseUUID = course;
    request.Index = index;
    request.AuthToken = window.AuthToken;

    let requestLoad = JSON.stringify(request);

    return tools.XHR(
        window.API_METHOD_POST,
        window.API_HOST_SCHEMES,
        window.API_HOST_HOST,
        window.API_URL_Chapter_CreateChapter,
        requestLoad
    );
};

window.Action_chapter_Delete = function (uuid) {
    let request = tools.DeepCopy(window.API_Request_Chapter_DeleteChapter);
    request.WillDeleteUUID = uuid;
    request.AuthToken = window.AuthToken;

    let requestLoad = JSON.stringify(request);

    return tools.XHR(
        window.API_METHOD_POST,
        window.API_HOST_SCHEMES,
        window.API_HOST_HOST,
        window.API_URL_Chapter_DeleteChapter,
        requestLoad
    );
};

window.Action_chapter_GetChapter = function (uuid) {
    let request = tools.DeepCopy(window.API_Request_Chapter_GetChapter);
    request.ChapterUUID = uuid;
    request.AuthToken = window.AuthToken;

    return tools.XHR(
        window.API_METHOD_GET,
        window.API_HOST_SCHEMES,
        window.API_HOST_HOST,
        tools.StrFormat(window.API_URL_Chapter_GetChapter, [
            request.ChapterUUID,
            request.AuthToken
        ]),
    );
};

window.Action_chapter_GetContent = function (uuid) {
    let request = tools.DeepCopy(window.API_Request_Chapter_GetContent);
    request.ChapterUUID = uuid;
    request.AuthToken = window.AuthToken;

    return tools.XHR(
        window.API_METHOD_GET,
        window.API_HOST_SCHEMES,
        window.API_HOST_HOST,
        tools.StrFormat(window.API_URL_Chapter_GetContent, [
            request.ChapterUUID,
            request.AuthToken
        ]),
    );
};

window.Action_chapter_List = function (courseUUID) {
    let request = tools.DeepCopy(window.API_Request_Chapter_ListChapter);
    request.CourseUUID = courseUUID;
    request.AuthToken = window.AuthToken;

    return tools.XHR(
        window.API_METHOD_GET,
        window.API_HOST_SCHEMES,
        window.API_HOST_HOST,
        tools.StrFormat(window.API_URL_Chapter_ListChapter, [
            request.CourseUUID,
            request.AuthToken
        ]),
    );
};

window.Action_chapter_SetManager = function (chapterUUID, userUUID) {
    let request = tools.DeepCopy(window.API_Request_Chapter_SetChapterManager);
    request.WillSetChapterUUID = chapterUUID;
    request.WillSetManagerUUID = userUUID;
    request.AuthToken = window.AuthToken;

    let requestLoad = JSON.stringify(request);

    return tools.XHR(
        window.API_METHOD_POST,
        window.API_HOST_SCHEMES,
        window.API_HOST_HOST,
        window.API_URL_Chapter_SetChapterManager,
        requestLoad
    );
};

window.Action_chapter_UnSetManager = function (chapterUUID, userUUID) {
    let request = tools.DeepCopy(window.API_Request_Chapter_UnSetChapterManager);
    request.WillUnSetChapterUUID = chapterUUID;
    request.WillUnSetManagerUUID = userUUID;
    request.AuthToken = window.AuthToken;

    let requestLoad = JSON.stringify(request);

    return tools.XHR(
        window.API_METHOD_POST,
        window.API_HOST_SCHEMES,
        window.API_HOST_HOST,
        window.API_URL_Chapter_UnSetChapterManager,
        requestLoad
    );
};

window.Action_chapter_UpdateChapter = function (uuid, name, description) {
    let request = tools.DeepCopy(window.API_Request_Chapter_UpdateChapter);
    request.NewName = name;
    request.NewDescription = description;
    request.WillUpdateUUID = uuid;
    request.AuthToken = window.AuthToken;

    let requestLoad = JSON.stringify(request);

    return tools.XHR(
        window.API_METHOD_POST,
        window.API_HOST_SCHEMES,
        window.API_HOST_HOST,
        window.API_URL_Chapter_UpdateChapter,
        requestLoad
    );
};

window.Action_chapter_UpdateContent = function (uuid, content) {
    let request = tools.DeepCopy(window.API_Request_Chapter_UpdateContent);
    request.MarkdownBase64 = tools.Base64(content);
    request.WillUpdateUUID = uuid;
    request.AuthToken = window.AuthToken;

    let requestLoad = JSON.stringify(request);

    return tools.XHR(
        window.API_METHOD_POST,
        window.API_HOST_SCHEMES,
        window.API_HOST_HOST,
        window.API_URL_Chapter_UpdateContent,
        requestLoad
    );
};
